package top.fzqblog.kotlinapp.activity

import android.content.Intent
import android.os.Bundle
import android.os.SystemClock
import android.support.v7.app.AppCompatActivity
import android.text.Editable
import android.text.SpannableString
import android.text.Spanned
import android.text.TextWatcher
import android.text.style.AbsoluteSizeSpan
import android.view.View
import kotlinx.android.synthetic.main.activity_login.*
import org.jetbrains.anko.db.insert
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.progressDialog
import org.jetbrains.anko.sdk25.coroutines.onClick
import org.jetbrains.anko.uiThread
import top.fzqblog.kotlinapp.R
import top.fzqblog.kotlinapp.func.log
import top.fzqblog.kotlinapp.func.startActivity
import top.fzqblog.kotlinapp.util.AnkoDBHelper
import top.fzqblog.kotlinapp.util.addUnionId
import top.fzqblog.kotlinapp.util.login

/**
 * Created by 抽离 on 2017/11/14 0014.
 */
class LoginActivity: AppCompatActivity() {

    fun initView(){
        // 新建一个可以添加属性的文本对象
        var telss: SpannableString = SpannableString("请输入手机号")
        // 新建一个属性对象,设置文字的大小
        var ass: AbsoluteSizeSpan = AbsoluteSizeSpan(15, true)
        // 附加属性到文本
        telss.setSpan(ass, 0, telss.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        tel.hint = telss

        var codess: SpannableString = SpannableString("请输入短信验证码")
        codess.setSpan(ass, 0, codess.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)

        code.hint = codess


        tel.addTextChangedListener(object: TextWatcher{
            override fun afterTextChanged(p0: Editable?) {
            }

            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

            override fun onTextChanged(c: CharSequence?, start: Int, count: Int, after: Int) {
                log("onTextChanged------" + c?.toString() + "---------")
                if(c.isNullOrEmpty()){
                    telClear.visibility = View.INVISIBLE
                }
                else{
                    telClear.visibility = View.VISIBLE
                }
            }
        })

        wxLogin.onClick {
            val intent = Intent(this@LoginActivity, AuthActivity::class.java)
            startActivityForResult(intent, 1);
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(resultCode == 1){
            val unionId = "o786EwUMuboHq2InhatnksvIlpfs"
            addUnionId(unionId)
            doAsync {
                val flag = login()
                uiThread {
                    if(flag){
                        startActivity<MainActivity>()
                        finish()
                    }
                }
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
        initView()
    }
}